import moment from 'moment'

function timeToStrFull(time){
	return timeToStr(time, "YYYY-MM-DD HH:mm:ss")
}

function timeToStr(time, format){
	return moment(time).format(format)
}

function strToTimeFull(str){
	return moment(str, "YYYY-MM-DD HH:mm:ss").valueOf()
}

function strToTime(str, format){
	return moment(str, format).valueOf()
}

function statusToText(status){
	switch(status){
		case 'all': return '全部'
		case 'created': return '待审核'
		case 'allowed': return '待调度'
		case 'dispatched': return '待出发'
		case 'travelStarted': return '已出发'
		case 'travelEnded': return '已到达'
		case 'finished': return '已完成'
		case 'cancled': return '已取消'
		case 'refused': return '未通过'
		case 'closed': return '已关闭'
		case 'adminComfirmed': return '待确认'
	}
}
// const baseUrl = 'http://47.108.198.55:9002'
// const fileUrl = 'http://47.108.198.55:8080/Files'

const baseUrl = 'http://192.168.101.7:8080'
const fileUrl = 'http://192.168.101.7:9002/Files'
function req(url,data){
	return new Promise((resolve, reject) => {
		console.log('------- req -------')
		console.log(url)
		console.log(data)
		wx.request({
		  url: `${baseUrl}${url}`, 
		  method:'POST',
		  data: data,
		  header: {
		    'content-type': 'application/json' 
		  },
		  success (res) {
			console.log('------- rsp success -------')
		    console.log(res.data)
			if(res.data.code == 101){
				uni.reLaunch({
					url: '../login-main/login-main?msg=' + res.data.msg
				})
				return
			}
			if(res.data.code != 200 && reject){
				reject(res.data)
			}
			if(res.data.code == 200){
				resolve(res.data.data)
			}
		  },
		  fail(err){
			  console.log('------- rsp fail -------')
			  console.log(err)
			  reject({code:-1,msg:err.Msg})
		  }
		})
	})
}

export default {
	timeToStrFull,
	timeToStr,
	strToTime,
	strToTimeFull,
	statusToText,
	req,
	baseUrl,
	fileUrl
}